package com.example.mybatisplusdemo.web.controller;

import com.example.mybatisplusdemo.model.dto.UserProfileDTO;
import com.example.mybatisplusdemo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserProfileController {
    private final UserService userService;

    @GetMapping("/{userId}")
    public ResponseEntity<UserProfileDTO> getUserProfileData(@PathVariable Long userId) {
        UserProfileDTO data = userService.getUserProfileData(userId);
        if (data == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(data);
    }

    @PostMapping("/{userId}/analyze")
    public ResponseEntity<String> analyzeUserProfile(@PathVariable Long userId) {
        userService.analyzeUserProfile(userId);
        return ResponseEntity.ok("用户画像分析完成");
    }

    @GetMapping("/{userId}/tags")
    public ResponseEntity<List<String>> getUserTags(@PathVariable Long userId) {
        UserProfileDTO data = userService.getUserProfileData(userId);
        if (data == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(data.getUserTags());
    }

    @PutMapping("/{userId}")
    public ResponseEntity<String> updateUserData(
            @PathVariable Long userId,
            @RequestBody UserProfileDTO data) {

        userService.updateUserData(userId, data);
        return ResponseEntity.ok("用户数据更新成功");
    }
}